import utils, { bus } from './utils'
import { config } from './config'
import { tabUtils } from '../router/base'
import Constant from './constant'
/**
 * 工具类
 */
const constant = new Constant()
export default {
  install (Vue) {
    Vue.prototype.$c = constant
    Vue.prototype.$bus = bus
    Vue.prototype.$utils = utils
    Vue.prototype.$config = config
    Vue.prototype.$tab = tabUtils
    Vue.prototype.$f = {
      setValue (fApi, data) {
        if (!data) data = {}
        const re = {}
        fApi.rule.forEach(e => {
          re[e.field] = data[e.field] || ''
          fApi.setValue(re)
        })
      },
      resetFields (fApi, data) {
        this.setValue(fApi, data)
        Vue.nextTick(() => fApi.clearValidateState()) // 清除表单验证状态
      }
    }
    const dialog = Vue.prototype.$utils.dialog
    // 添加实例方法
    Vue.prototype.$utils.dialog = (title, content, options = {}) => {
      options.closeOnClickModal = false // 点击弹窗外不关闭弹窗
      const defaultOptions = { width: '95%' }
      return dialog(title, content, { ...defaultOptions, ...options })
    }
    // constant = Vue.prototype.$c
    // console.log(constant)
    // 简化表单校验写法的封装
    const getNumberMessage = (message, min = null, max = null) => {
      if (message) return message
      if (min === null && max === null) {
        return '数据格式不正确'
      }
      let re = '字段必须'
      if (min) {
        re += '大于等于' + min
      }
      if (max) {
        if (min) {
          re += '并且'
        }
        re += '小于等于' + max
      }
      return re
    }
    const getLengthMessage = (message, min = null, max = null) => {
      if (message) return message
      if (min === null && max === null) {
        return '数据格式不正确'
      }
      let re = '字符长度必须'
      if (max === min) {
        re = '等于' + min
      } else {
        if (min) {
          re += '大于等于' + min
        }
        if (max) {
          if (min) {
            re += '并且'
          }
          re += '小于等于' + max
        }
      }
      return re
    }
    Vue.prototype.$v = {
      required: (type = 'string', message = '字段不能为空') => ({ type, required: true, message }),
      type: (type = 'string', required, message = '数据格式不正确') => ({ type, required, message }),
      number: (min, max, message) => ({ type: 'number', min, max, message: getNumberMessage(message, min, max) }),
      integer: (min, max, message) => ({ type: 'integer', min, max, message: getNumberMessage(message, min, max) }),
      float: (min, max, message) => ({ type: 'float', min, max, message: getNumberMessage(message, min, max) }),
      length: (val, message) => ({ len: val, message: message || `字符长度必须等于${val}` }),
      lengthRange: (min, max, message) => ({ min, max, message: getLengthMessage(message, min, max) }),
      minLength: (val, message) => ({ type: 'string', min: val, message: message || `长度必须大于等于${val}` }),
      maxLength: (val, message) => ({ type: 'string', max: val, message: message || `长度必须小于等于${val}` }),
      email: (val, message = '数据格式不正确') => ({ type: 'email', message: message }),
      pattern: (pattern, message = '数据格式不正确') => ({ pattern, message: message }),
      validator: (validator, message) => ({ validator, message: message }),
      inputNumber: () => ({ type: 'number', min: 0, required: true, message: '最小为0' }),
      tel: (required = false) => ({ pattern: /^\d+$/, message: '数据格式不正确', required })
    }
  }
}
export { utils, bus, constant, tabUtils }
